package com.excesys.exsecs.om.job;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.excesys.exsecs.om.entity.OmICheckPlan;
import com.excesys.exsecs.om.entity.OmICheckPlanTime;
import com.excesys.exsecs.om.entity.OmICheckSheet;
import com.excesys.exsecs.om.entity.OmICheckUserRel;
import com.excesys.exsecs.om.mapper.OmICheckPlanMapper;
import com.excesys.exsecs.om.mapper.OmICheckSheetMapper;
import com.excesys.exsecs.om.mapper.SequenceMapper;
import com.excesys.exsecs.om.service.OmICheckPlanService;
import com.excesys.exsecs.om.service.OmICheckPlanTimeService;
import com.excesys.exsecs.om.service.OmICheckUserRelService;
import com.excesys.exsecs.om.utils.OrderNoUtils;
import com.excesys.exsecs.om.utils.PlanToSheetUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlanToSheetJob {

	private final OmICheckPlanService omICheckPlanService;

	private final OmICheckPlanMapper omICheckPlanMapper;

	private final OmICheckPlanTimeService omICheckPlanTimeService;

	private final OmICheckUserRelService omICheckUserRelService;

	private final OmICheckSheetMapper omICheckSheetMapper;

	private final OrderNoUtils orderNoUtils;

	private final SequenceMapper sequenceMapper;

	private final PlanToSheetUtils planToSheetUtils;

	@Scheduled(cron = "0 0 0 * * ? ")
	//@Scheduled(cron = "0/60 * * * * ?")
	public void createSheetJob() throws ParseException {
		log.info("this is task job");
		List<OmICheckPlan> list = omICheckPlanService.getPlanList();
		List<OmICheckPlan> planList = new ArrayList<>();
		for (OmICheckPlan plans : list) {
			if (StringUtil.isEmpty(plans.getPlanStartTime()) && StringUtil.isEmpty(plans.getPlanEndTime())) {
				planList.add(plans);
			}
			else {
				boolean flag = timeCalendar(
						new SimpleDateFormat("yyyy-MM-dd").parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date())),
						new SimpleDateFormat("yyyy-MM-dd").parse(plans.getPlanStartTime()),
						new SimpleDateFormat("yyyy-MM-dd").parse(plans.getPlanEndTime()));
				if (flag)
					planList.add(plans);
			}
		}
		log.info("获取今日巡检计划数：" + planList.size());
		log.info("获取今日巡检=：" + planList);
		for (OmICheckPlan plan : planList) {
			// 自定义计划
			if ("1".equals(plan.getPlanType())) {
				String[] date = plan.getExcuteDate().split(",");
				for (int i = 0; i < date.length; i++) {
					// 周期时间跟今天相等
					if (new SimpleDateFormat("yyyy-MM-dd").format(new Date()).equals(date[i])) {
//						int checkSeq = sequenceMapper.getSeqNextVal("checkseq");
//						String num = orderNoUtils.getOrderNumber("Check", checkSeq);
//						List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
//						for (OmICheckUserRel userRel : userList) {
//							userRel.setCheckNo(num);
//							omICheckUserRelService.saveOrUpdate(userRel);
//						}
						List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
						List<OmICheckPlanTime> planTimeList = omICheckPlanTimeService.listRelByPlanId(plan.getPlId());
						for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
							int checkSeqs = sequenceMapper.getSeqNextVal("checkseq");
							String nums = orderNoUtils.getOrderNumber("Check", checkSeqs);
							OmICheckSheet omICheckSheet = new OmICheckSheet();
							omICheckSheet.setCheckNo(nums);
							omICheckSheet.setPlanNo(plan.getPlanNo());
							omICheckSheet.setRouteId(plan.getRouteId());
							omICheckSheet.setCheckName(plan.getPlanName());
							omICheckSheet.setCheckUserName(plan.getCheckUserName());
							omICheckSheet.setCheckUserId(plan.getCheckUserId());
							omICheckSheet.setCheckDeptId(plan.getCheckDeptId());
							omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
							omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
							omICheckSheet.setSheetStatus("1");
							omICheckSheetMapper.insert(omICheckSheet);
							log.info("获取今日巡检插入数据：" + omICheckSheet);
//							List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
							for (OmICheckUserRel userRel : userList) {
								userRel.setId(null);
								userRel.setCheckNo(nums);
								userRel.setPlanNo("");
								omICheckUserRelService.save(userRel);
							}
						}
					}
				}
			}
			else {
				// 周期计划 0天/1周
				if ("0".equals(plan.getCycleType())) {
					Date d = new Date();
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					String date = format.format(d);
					log.info("现在的日期是：" + date);
					int day = plan.getFrequency();
					int size = plan.getFrequencyNum();
					String lastday = plan.getPlanStartTime();

					Calendar ca = Calendar.getInstance();
					ca.setTime(format.parse(lastday));
					if (day != 1 && size == 1)
						ca.add(Calendar.DATE, day);// day为增加的天数
					else
						ca.add(Calendar.DATE, day * size);// day为增加的天数
					d = ca.getTime();
					String backTime = format.format(d);
					log.info("增加天数以后的日期：" + backTime);
					if (date.equals(backTime) || day == 1) {
//						int checkSeq = sequenceMapper.getSeqNextVal("checkseq");
//						String num = orderNoUtils.getOrderNumber("Check", checkSeq);
//						List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
//						for (OmICheckUserRel userRel : userList) {
//							userRel.setCheckNo(num);
//							omICheckUserRelService.saveOrUpdate(userRel);
//						}
						List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
						List<OmICheckPlanTime> planTimeList = omICheckPlanTimeService.listRelByPlanId(plan.getPlId());
						for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
							int checkSeqs = sequenceMapper.getSeqNextVal("checkseq");
							String nums = orderNoUtils.getOrderNumber("Check", checkSeqs);
							OmICheckSheet omICheckSheet = new OmICheckSheet();
							omICheckSheet.setCheckNo(nums);
							omICheckSheet.setPlanNo(plan.getPlanNo());
							omICheckSheet.setRouteId(plan.getRouteId());
							omICheckSheet.setCheckName(plan.getPlanName());
							omICheckSheet.setCheckUserId(plan.getCheckUserId());
							omICheckSheet.setCheckDeptId(plan.getCheckDeptId());
							omICheckSheet.setCheckUserName(plan.getCheckUserName());
							omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
							omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
							omICheckSheet.setSheetStatus("1");
							omICheckSheetMapper.insert(omICheckSheet);
							log.info("获取今日巡检插入数据：" + omICheckSheet);
						//	List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
							for (OmICheckUserRel userRel : userList) {
								userRel.setId(null);
								userRel.setCheckNo(nums);
								userRel.setPlanNo("");
								omICheckUserRelService.save(userRel);
							}
						}
						size = size + 1;
						plan.setFrequencyNum(size);
						omICheckPlanMapper.updateById(plan);
					}
				}
				else if ("1".equals(plan.getCycleType())) {
					Date d = new Date();
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					String date = format.format(d);
					log.info("现在的日期是：" + date);
					// 几周
					int day = plan.getFrequency();
					String lastday = plan.getPlanStartTime();

					Calendar ca = Calendar.getInstance();
					ca.setTime(format.parse(lastday));
					ca.add(Calendar.DATE, day * 7);// day*7 表示几周
					d = ca.getTime();
					String backTime = format.format(d);
					log.info("增加天数以后的日期：" + backTime);
					if (date.equals(backTime)) {
//						int checkSeq = sequenceMapper.getSeqNextVal("checkseq");
//						String num = orderNoUtils.getOrderNumber("Check", checkSeq);
//						List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
//						for (OmICheckUserRel userRel : userList) {
//							userRel.setCheckNo(num);
//							omICheckUserRelService.saveOrUpdate(userRel);
//						}
						List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
						List<OmICheckPlanTime> planTimeList = omICheckPlanTimeService.listRelByPlanId(plan.getPlId());
						for (OmICheckPlanTime omICheckPlanTime : planTimeList) {
							int checkSeqs = sequenceMapper.getSeqNextVal("checkseq");
							String nums = orderNoUtils.getOrderNumber("Check", checkSeqs);
							OmICheckSheet omICheckSheet = new OmICheckSheet();
							omICheckSheet.setCheckNo(nums);
							omICheckSheet.setPlanNo(plan.getPlanNo());
							omICheckSheet.setRouteId(plan.getRouteId());
							omICheckSheet.setCheckName(plan.getPlanName());
							omICheckSheet.setCheckUserName(plan.getCheckUserName());
							omICheckSheet.setCheckUserId(plan.getCheckUserId());
							omICheckSheet.setCheckDeptId(plan.getCheckDeptId());
							omICheckSheet.setExcuteStartTime(omICheckPlanTime.getExcuteStartTime());
							omICheckSheet.setExcuteEndTime(omICheckPlanTime.getExcuteEndTime());
							omICheckSheet.setSheetStatus("1");
							omICheckSheetMapper.insert(omICheckSheet);
							log.info("获取今日巡检插入数据：" + omICheckSheet);
//							List<OmICheckUserRel> userList = omICheckUserRelService.getUserList(plan.getPlanNo());
							for (OmICheckUserRel userRel : userList) {
								userRel.setId(null);
								userRel.setCheckNo(nums);
								userRel.setPlanNo("");
								omICheckUserRelService.save(userRel);
							}
						}
					}
				}
			}
		}
	}

	public static boolean timeCalendar(Date nowTime, Date beginTime, Date endTime) {
		// 设置当前时间
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);
		// 设置开始时间
		Calendar begin = Calendar.getInstance();
		begin.setTime(beginTime);// 开始时间
		// 设置结束时间
		Calendar end = Calendar.getInstance();
		end.setTime(endTime);// 上午结束时间
		// 处于开始时间之后，和结束时间之前的判断
		if (((date.after(begin) || date.equals(begin)) && (date.before(end) || date.equals(end)))) {
			return true;
		}
		else {
			return false;
		}
	}

}
